package com.lepu.app.fun.index.adapter;

import java.util.ArrayList;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.lepu.app.main.activity.AppImageBroswerActivity;
import com.lepu.pasm.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

@SuppressLint("InflateParams")
public class IndexFileReportGridAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater = null;
	public Context context = null;
	public ArrayList<String> arrayList = null;

	public IndexFileReportGridAdapter(Context c, ArrayList<String> list) {
		context = c;
		layoutInflater = LayoutInflater.from(c);
		arrayList = list;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (arrayList != null && arrayList.size() > 0)
			size = arrayList.size();
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (arrayList == null || arrayList.size() == 0)
			return null;
		final ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.index_file_report_grid_item, null);

			holder = new ViewHolder();
			ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);

			holder.imageView = imageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		UniversalImageLoadTool.disPlay(arrayList.get(position),holder.imageView,R.drawable.app_icon_load_image);
		holder.imageView.setOnClickListener(imageClickListener((String[]) arrayList.toArray(new String[arrayList.size()]), position+1));
		
		return convertView;
	}

	// 图片监听
	private View.OnClickListener imageClickListener(final String[] urlArray,final int position) {
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,AppImageBroswerActivity.class);
				intent.putExtra("isShowTopbar", false);
				intent.putExtra("index", position - 1);
				intent.putExtra("urlArray", urlArray);
				((BaseFragmentActivity) context).startActivity(intent);
			}
		};
	}

	static class ViewHolder {
		ImageView imageView;
	}
}
